# 超时
# class Solution(object):
#     def findMaximumXOR(self, nums):
#         """
#         :type nums: List[int]
#         :rtype: int
#         """
#         self.max_val = -float('inf')
#         nums = set(nums)
#         if len(nums) == 1:
#             return 0
#         t = 30
#         set1 = set()
#         set2 = set()
#         while not set1 or not set2:
#             set1 = set()
#             set2 = set()
#             for key in nums:
#                 if (key >> t) & 1 >= 1:
#                     set1.add(key)
#                 else:
#                     set2.add(key)
#             t -= 1
#
#         def search(set_1, set_2, t):
#             if t < 0:
#                 self.max_val = max(set_1.pop() ^ set_2.pop(), self.max_val)
#             else:
#                 if len(set_1) == 1 and len(set_2) == 1:
#                     self.max_val = max(set_1.pop() ^ set_2.pop(), self.max_val)
#                 else:
#                     set_11 = set()
#                     set_12 = set()
#                     set_21 = set()
#                     set_22 = set()
#                     for key in set_1:
#                         if (key >> t) & 1 >= 1:
#                             set_11.add(key)
#                         else:
#                             set_12.add(key)
#                     for key in set_2:
#                         if (key >> t) & 1 >= 1:
#                             set_21.add(key)
#                         else:
#                             set_22.add(key)
#                     if set_11 and set_22:
#                         search(set_11, set_22, t - 1)
#                     if set_12 and set_21:
#                         search(set_12, set_21, t - 1)
#                     if (not (set_11 and set_22)) and (not (set_12 and set_21)):
#                         search(set_1, set_2, t - 1)

class Solution(object):
    def findMaximumXOR(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        s = set(nums)
        n = len(nums)
        if n == 1:
            return 0
        t = 30
        prex = 0
        while t >= 0:
            prex |= (1 << t)
            flag = False
            new_s = set()
            for num in s:
                new_s.add(num >> t)
            for num in new_s:
                if (prex >> t) ^ num in new_s:
                    flag = True
                    break
            if not flag:
                prex &= (prex - 1)
            t -= 1
        return prex

data = Solution()
nums = [3, 10, 5, 25, 2, 8]
print(data.findMaximumXOR(nums))